﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Test_API
{
    static class ExtenstionMethods
    {
        // http://stackoverflow.com/a/3854950/385205
        private static readonly Random getrandom = new Random();
        private static readonly object syncLock = new object();

        /// <summary>
        /// Returns a random number as: min &lt;= n &lt; max
        /// </summary>
        public static int randomNumber(int min, int max)
        {
            lock (syncLock)
            { // synchronize
                return getrandom.Next(min, max);
            }
        }
        /// <summary>
        /// Returns a random number as: 0 &lt;= n &lt; count
        /// </summary>
        public static int randomNumber(int count)
        {
            lock (syncLock)
            {
                return getrandom.Next(count);
            }
        }

        public static T randomElement<T>(this List<T> inputs)
        {
            int count = inputs.Count;
            if (count == 0)
            {
                return default(T);
            }
            return inputs[ExtenstionMethods.randomNumber(count)];
        }

        public static string buildQueryString(this Dictionary<string, string> inputs)
        {
            string result = String.Empty;

            foreach (var item in inputs)
            {
                result += item.Key + "=" + HttpUtility.UrlEncode(item.Value) + "&";
            }
            return result;
        }

        public static long convertToUnixtime(this DateTime dt)
        {
            long result = 0;
            if (dt == null)
                return result;

            TimeSpan span = (dt.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc));
            result = (long)span.TotalSeconds;

            return result;
        }

        public static string StringToCSVCell(this string str)
        {
            if (str == null || str == String.Empty)
            {
                return String.Empty;
            }
            bool mustQuote = (str.Contains(",") || str.Contains("\"") || str.Contains("\r") || str.Contains("\n"));
            if (mustQuote)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("\"");
                foreach (char nextChar in str)
                {
                    sb.Append(nextChar);
                    if (nextChar == '"')
                        sb.Append("\"");
                }
                sb.Append("\"");
                return sb.ToString();
            }

            return str;
        }

        public static Dictionary<string, object> urlEncodeValues(this Dictionary<string, object> inputs)
        {
            Dictionary<string, object> newElements = new Dictionary<string, object>();
            foreach (var item in inputs)
            {
                System.Text.Encoding srcEnc = System.Text.Encoding.ASCII;
                System.Text.Encoding destEnc = System.Text.Encoding.UTF8;
                byte[] bData = srcEnc.GetBytes(item.Value.ToString());
                byte[] bResult = System.Text.Encoding.Convert(srcEnc, destEnc, bData);
                string convertedText = destEnc.GetString(bResult);
                //newElements.Add(item.Key, HttpUtility.UrlEncode(item.Value.ToString()));
                newElements.Add(item.Key, convertedText);
            }

            return newElements;
        }

    }
}
